#include "systemtray.h"

SystemTray::SystemTray(QObject *parent)
    : QObject(parent)
{
    // 创建托盘组件
    trayIcon = new QSystemTrayIcon(this);
    trayMenu = new QMenu();
    restoreAction = new QAction("恢复", this);
    quitAction = new QAction("退出", this);

    // 设置图标
    trayIcon->setIcon(QIcon(":/icon/images/weatherForecast.ico"));
    trayIcon->setToolTip("Weather Forecast");
    restoreAction->setIcon(QIcon(":/icon/images/restore.ico"));
    quitAction->setIcon(QIcon(":/icon/images/exit.ico"));

    // 组装菜单
    trayMenu->addAction(restoreAction);
    trayMenu->addSeparator();
    trayMenu->addAction(quitAction);
    trayIcon->setContextMenu(trayMenu);

    // 连接信号槽
    connect(restoreAction, &QAction::triggered, this, &SystemTray::restoreHandler);
    connect(quitAction, &QAction::triggered, this, &SystemTray::quitHandler);
    connect(trayIcon, &QSystemTrayIcon::activated, this, &SystemTray::onTrayActivated);
}

void SystemTray::setupTrayIcon(QWidget *trayWindow) {
    this->trayWindow = trayWindow;
    trayIcon->show();   // 显示系统托盘
}

void SystemTray::onTrayActivated(QSystemTrayIcon::ActivationReason reason)
{
    if (reason == QSystemTrayIcon::DoubleClick) {
        emit restoreHandler();
    }
}
